/*	
	
	Copyright (c) 2007-2009 Ryan Christiansen
	
	This software is provided 'as-is', without any express or implied
	warranty. In no event will the authors be held liable for any damages
	arising from the use of this software.
	
	Permission is granted to anyone to use this software for any purpose,
	including commercial applications, and to alter it and redistribute it
	freely, subject to the following restrictions:
	
	1. The origin of this software must not be misrepresented; you must not
	claim that you wrote the original software. If you use this software
	in a product, an acknowledgment in the product documentation would be
	appreciated but is not required.
	
	2. Altered source versions must be plainly marked as such, and must not be
	misrepresented as being the original software.
	
	3. This notice may not be removed or altered from any source
	distribution.
	
*/
package funit.framework
{
	import funit.errors.AssertionError;
	import funit.errors.IgnoreError;
	import funit.framework.comparers.AssertEqualityComparer;
	import funit.framework.writers.EqualMessageWriter;
	import funit.framework.writers.MessageWriter;
	import funit.framework.writers.NotEqualMessageWriter;
	
	import mx.utils.StringUtil;
	
	import sv.collections.IEqualityComparer;
	import sv.utils.ClassUtil;
	
	public class Assert
	{
		
		/**
		 * @param condition
		 * @param message
		 * @param messageParameters
		 */
		public static function isTrue( condition:Boolean, message:String = "", ... messageParameters ) : void
		{
			if ( !condition )
			{
				var writer:MessageWriter = new MessageWriter( message, messageParameters );
				writer.writeExpectedLine(true);
				writer.writeActualLine(condition);
				
				throw new AssertionError( writer.toString() );
			}
		}
		
		/**
		 * @param condition
		 * @param message
		 * @param messageParameters
		 */
		public static function isFalse( condition:Boolean, message:String = "", ... messageParameters ) : void
		{
			if ( condition )
			{
				var writer:MessageWriter = new MessageWriter( message, messageParameters );
				writer.writeExpectedLine(false);
				writer.writeActualLine(condition);
				
				throw new AssertionError( writer.toString() );
			}
		}
		
		/**
		 * @param expected
		 * @param actual
		 * @param message
		 * @param messageParameters
		 */
		public static function areEqual( expected:Object, actual:Object, message:String = "", ... messageParameters ) : void
		{
			var comparer:IEqualityComparer = new AssertEqualityComparer();
			
			if ( !comparer.equals(expected, actual) )
			{
				var writer:EqualMessageWriter = new EqualMessageWriter( message, messageParameters );
				writer.displayDifferences(expected, actual);
				
				throw new AssertionError( writer.toString() );
			}
		}
		
		/**
		 * @param expected
		 * @param actual
		 * @param message
		 * @param messageParameters
		 */
		public static function areNotEqual( expected:Object, actual:Object, message:String = "", ... messageParameters ) : void
		{
			var comparer:IEqualityComparer = new AssertEqualityComparer();
			
			if ( comparer.equals(expected, actual) )
			{
				var writer:NotEqualMessageWriter = new NotEqualMessageWriter( message, messageParameters );
				writer.displayDifferences(expected, actual);
				
				throw new AssertionError( writer.toString() );
			}
		}
		
		/**
		 * @param expected
		 * @param actual
		 * @param message
		 * @param messageParameters
		 */
		public static function areSame( expected:Object, actual:Object, message:String = "", ... messageParameters ) : void
		{
			if ( expected !== actual )
			{
				var writer:MessageWriter = new MessageWriter( message, messageParameters );
				writer.writeExpectedLine(expected);
				writer.writeActualLine(actual);
				
				throw new AssertionError( writer.toString() );
			}
		}
		
		/**
		 * @param expected
		 * @param actual
		 * @param message
		 * @param messageParameters
		 */
		public static function areNotSame( expected:Object, actual:Object, message:String = "", ... messageParameters ) : void
		{
			if ( expected === actual )
			{
				var writer:MessageWriter = new MessageWriter( message, messageParameters );
				writer.writeExpectedLine(expected, "not");
				writer.writeActualLine(actual);
				
				throw new AssertionError( writer.toString() );
			}
		}
		
		/**
		 * @param instance
		 * @param message
		 * @param messageParameters
		 */
		public static function isNull( instance:Object, message:String = "", ... messageParameters ) : void
		{
			if ( instance != null )
			{
				var writer:MessageWriter = new MessageWriter( message, messageParameters );
				writer.writeExpectedLine(null);
				writer.writeActualLine(instance);
				
				throw new AssertionError( writer.toString() );
			}
		}
		
		/**
		 * @param instance
		 * @param message
		 * @param messageParameters
		 */
		public static function isNotNull( instance:Object, message:String = "", ... messageParameters ) : void
		{
			if ( instance == null )
			{
				var writer:MessageWriter = new MessageWriter( message, messageParameters );
				writer.writeExpectedLine(null, "not");
				writer.writeActualLine(instance);
				
				throw new AssertionError( writer.toString() );
			}
		}
		
		/**
		 * @param instance
		 * @param message
		 * @param messageParameters
		 */
		public static function isUndefined( instance:*, message:String = "", ... messageParameters ) : void
		{
			if ( instance !== undefined )
			{
				fail(message, messageParameters);
			}
		}
		
		/**
		 * @param instance
		 * @param message
		 * @param messageParameters
		 */
		public static function isNotUndefined( instance:*, message:String = "", ... messageParameters ) : void
		{
			if ( instance === undefined )
			{
				fail(message, messageParameters);
			}
		}
		
		/**
		 * @param definition
		 * @param instance
		 * @param message
		 * @param messageParameters
		 */
		public static function isInstanceOf( definition:Class, instance:Object, message:String = "", ... messageParameters ) : void
		{
			if ( !(instance is definition) )
			{
				var writer:MessageWriter = new MessageWriter( message, messageParameters );
				writer.writeExpectedLine(definition, "instance of");
				writer.writeActualLine(
					(instance != null) ? ClassUtil.getClass(instance) : null
				);
				
				throw new AssertionError( writer.toString() );
			}
		}
		
		/**
		 * @param definition
		 * @param instance
		 * @param message
		 * @param messageParameters
		 */
		public static function isNotInstanceOf( definition:Class, instance:Object, message:String = "", ... messageParameters ) : void
		{
			if ( instance is definition )
			{
				var writer:MessageWriter = new MessageWriter( message, messageParameters );
				writer.writeExpectedLine(definition, "not instance of");
				writer.writeActualLine(
					(instance != null) ? ClassUtil.getClass(instance) : null
				);
				
				throw new AssertionError( writer.toString() );
			}
		}
		
		/**
		 * @param value
		 * @param message
		 * @param messageParameters
		 */
		public static function isFinite( value:Number, message:String = "", ... messageParameters ) : void
		{
			if ( !NumberUtil.getIsFinite(value) )
			{
				var writer:MessageWriter = new MessageWriter( message, messageParameters );
				writer.writeLine( MessageString.ExpectedPrefix + "finite Number" );
				writer.writeActualLine(value);
				
				throw new AssertionError( writer.toString() );
			}
		}
		
		/**
		 * @param value
		 * @param message
		 * @param messageParameters
		 */
		public static function isNaN( value:Number, message:String = "", ... messageParameters ) : void
		{
			if ( !NumberUtil.getIsNaN(value) )
			{
				var writer:MessageWriter = new MessageWriter( message, messageParameters );
				writer.writeExpectedLine(NaN);
				writer.writeActualLine(value);
				
				throw new AssertionError( writer.toString() );
			}
		}
		
		/**
		 * @param expectedGreater
		 * @param expectedLess
		 * @param message
		 * @param messageParameters
		 */
		public static function isGreater( expectedGreater:Object, expectedLess:Object, message:String = "", ... messageParameters ) : void
		{
			if ( expectedGreater <= expectedLess )
			{
				fail(message, messageParameters);
			}
		}
		
		/**
		 * @param expectedGreaterOrEqual
		 * @param expectedLess
		 * @param message
		 * @param messageParameters
		 */
		public static function isGreaterOrEqual( expectedGreaterOrEqual:Object, expectedLess:Object, message:String = "", ... messageParameters ) : void
		{
			if ( expectedGreaterOrEqual < expectedLess )
			{
				fail(message, messageParameters);
			}
		}
		
		/**
		 * @param expectedLess
		 * @param expectedGreater
		 * @param message
		 * @param messageParameters
		 */
		public static function isLess( expectedLess:Object, expectedGreater:Object, message:String = "", ... messageParameters ) : void
		{
			if ( expectedLess >= expectedGreater )
			{
				fail(message, messageParameters);
			}
		}
		
		/**
		 * @param expectedLessOrEqual
		 * @param expectedGreater
		 * @param message
		 * @param messageParameters
		 */
		public static function isLessOrEqual( expectedLessOrEqual:Object, expectedGreater:Object, message:String = "", ... messageParameters ) : void
		{
			if ( expectedLessOrEqual > expectedGreater )
			{
				fail(message, messageParameters);
			}
		}
		
		/**
		 * @param message
		 * @param messageParameters
		 */
		public static function fail( message:String = "", ... messageParameters ) : void
		{
			throw	new AssertionError(
						StringUtil.substitute(message, messageParameters)
					);
		}
		
		/**
		 * @param message
		 * @param messageParameters
		 */
		public static function ignore( message:String = "", ... messageParameters ) : void
		{
			throw	new IgnoreError(
						StringUtil.substitute(message, messageParameters)
					);
		}
		
	}
	
}

/**
 * Internal helper class to bypass global naming conflicts.
 * @param value
 * @return
 */
internal class NumberUtil
{
	
	internal static function getIsFinite( value:Number ) : Boolean
	{
		return isFinite(value);
	}
	
	internal static function getIsNaN( value:Number ) : Boolean
	{
		return isNaN(value);
	}
	
}